#include "wpredefinedvolumes.h"

wPredefinedVolumes::wPredefinedVolumes()
{
    //ctor
}

wPredefinedVolumes::~wPredefinedVolumes()
{
    //dtor
}

bool wPredefinedVolumes::readFromFile(string jsonfilename,string& error)
{
    ifstream ifs( jsonfilename.c_str() ) ;
    if( ifs.good() )
    {
        DynamicJsonBuffer jsonBuffer;
        JsonObject& root = jsonBuffer.parseObject(ifs);
        bool parseOk = root.success() ;//检查解析json是否成功
        if( parseOk==true )
        {
            JsonArray& volarray = root["volumes"].asArray() ;
            if( volarray.success() ) {
                    for(int i = 0 ; i<volarray.size();++i ) {
                        JsonObject& obj1 = volarray[i].asObject();
                        string tag1 = obj1["tag"].as<char*>() ;
                        string hostdir = obj1["hostdir"].as<char*>() ;
                        _volumes[tag1] = hostdir ;
                    }
                    return true;
            }else{
                error = string("parse volumes failed : ") + jsonfilename ;
                return false ;
            }

        }else{
            error = string("parse json failed : ") + jsonfilename ;
            return false ;
        }
    }else
    {
        error = string("open json file failed : ") + jsonfilename  ;
        return false ;
    }
}
string wPredefinedVolumes::getHostDirByTag(string tag) {
    try {
        return _volumes[tag] ;
    }catch( const std::out_of_range & ex ) {
        return string("");
    }
}
